//
//  AddressListTableViewController.m
//  AddressList
//
//  Created by joker on 15/7/23.
//  Copyright (c) 2015年 joker. All rights reserved.
//

#import "AddressListTableViewController.h"
#import "Constants.h"
#import "WebService.h"
#import  "ServerRequestUtility.h"

@implementation AddressListTableViewController

-(void) viewDidLoad{
    [super viewDidLoad];
#ifdef DUMP_INFO
    NSLog(@"%@", self.userInfo);
#endif
}

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.loadDataStatusIndicator startAnimating];
    [self pullDepartmentStructureInfo];
    [self pullEmployeeInfo];
    
}

#pragma mark - Get information from server functions
-(void)pullDepartmentStructureInfo{
    NSDictionary * params = @{
                              kOAfUserID: self.userInfo[kOAfUserID],
                              kAccessKey: self.userInfo[kAccessKey]
                              };
    WebService *server = [[WebService alloc] initWithURL:[NSURL URLWithString:DepartmentStructureAPI relativeToURL:[NSURL URLWithString:serverSite]] AndRequestParam:params Delegate:self];
    server.type = GetDepartmentStructure;
}
-(void)pullEmployeeInfo{
    NSDictionary * params = @{
                              kOAfUserID: self.userInfo[kOAfUserID],
                              kAccessKey: self.userInfo[kAccessKey]
                              };
    WebService *server = [[WebService alloc] initWithURL:[NSURL URLWithString:EmployeeInfoAPI relativeToURL:[NSURL URLWithString:serverSite]] AndRequestParam:params Delegate:self];
    server.type = GetEmployeeInfo;
}
#pragma mark - WebService Delegate
-(void)processReturnValue:(WebService *)server ReturnValue:(NSString *)returnValue{
    NSDictionary *temp;
    NSArray * tempArr;
    switch (server.type) {
        case GetDepartmentStructure:
        temp = [NSJSONSerialization JSONObjectWithData:[returnValue dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
            
#ifdef DUMP_INFO
            NSLog(@"GetDepartmentStructureInfo = \n %@",temp);
#endif
            break;
        case GetEmployeeInfo:
            tempArr = [NSJSONSerialization JSONObjectWithData:[returnValue dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
#ifdef DUMP_INFO
            NSLog(@"GetEmployeeInfo = \n %lu", (unsigned long) [tempArr count]);
             [ServerRequestUtility showAlertWithTitle:@"" MessageBody:[NSString stringWithFormat:@"There are %lu Employee",(unsigned long)[tempArr count]]];
#endif
           
                  
            break;
        default:
            break;
    }
    [self.loadDataStatusIndicator stopAnimating];
}
@end
